/**
 * 实体层统一导出
 */

// 基础实体
export { BaseEntity } from './BaseEntity';

// 核心实体
export { Player } from './Player';
export type { 
    PlayerAttributes, 
    PlayerStats, 
    PlayerEquipment, 
    PlayerPosition, 
    PlayerSettings, 
    LevelUpResult 
} from './Player';
export { PlayerState } from './Player';

export { Item } from './Item';
export type { 
    ItemData, 
    ItemCreateConfig, 
    StackResult, 
    UseResult, 
    ItemEffect 
} from './Item';

export { Skill } from './Skill';
export type { 
    SkillEffect, 
    SkillRequirements, 
    SkillData, 
    SkillCreateConfig,
    LevelUpResult as SkillLevelUpResult,
    UseResult as SkillUseResult,
    RequirementCheckResult as SkillRequirementCheckResult
} from './Skill';
export { SkillType, SkillTarget } from './Skill';

export { Equipment } from './Equipment';
export type { 
    EquipmentAttribute, 
    EquipmentSkill, 
    EquipmentData, 
    EquipmentCreateConfig,
    EnhanceResult,
    RepairResult,
    EquipCheckResult
} from './Equipment';
// export { EquipmentType, EquipmentSlot, EquipmentRarity } from './Equipment'; // 暂时注释

export { Task } from './Task';
export type { 
    TaskTarget, 
    TaskReward, 
    TaskData, 
    TaskCreateConfig,
    CompleteResult,
    ProgressResult,
    // TaskProgressDetails,
    // TaskStatistics
} from './Task';
export { TaskType, TaskStatus, TaskTargetType } from './Task';
// export { TaskDifficulty } from './Task'; // 暂时注释

// 通用类型
export type EntityId = string;
export type EntityType = 'player' | 'item' | 'skill' | 'equipment' | 'task';

/**
 * 实体工厂类
 */
export class EntityFactory {
    /**
     * 创建玩家实体
     */
    static createPlayer(config?: any): any {
        const playerInstance = new (require('./Player').Player)();
        if (config) {
            Object.assign(playerInstance, config);
        }
        return playerInstance;
    }

    /**
     * 创建物品实体
     */
    static createItem(config: any): any {
        const ItemClass = require('./Item').Item;
        return ItemClass.create(config);
    }

    /**
     * 创建技能实体
     */
    static createSkill(config: any): any {
        const SkillClass = require('./Skill').Skill;
        return SkillClass.create(config);
    }

    /**
     * 创建装备实体
     */
    static createEquipment(config: any): any {
        const EquipmentClass = require('./Equipment').Equipment;
        return EquipmentClass.create(config);
    }

    /**
     * 创建任务实体
     */
    static createTask(config: any): any {
        const TaskClass = require('./Task').Task;
        return TaskClass.create(config);
    }
}

/**
 * 实体验证器类
 */
export class EntityValidator {
    /**
     * 验证实体
     */
    static validate(entity: any): boolean {
        if (!entity || typeof entity !== 'object') {
            return false;
        }

        // 基础验证
        return typeof entity.id === 'string' && entity.id.length > 0;
    }

    /**
     * 批量验证实体
     */
    static validateBatch(entities: any[]): {
        valid: any[];
        invalid: { entity: any; errors: any[] }[];
    } {
        const valid: any[] = [];
        const invalid: { entity: any; errors: any[] }[] = [];

        entities.forEach(entity => {
            if (this.validate(entity)) {
                valid.push(entity);
            } else {
                invalid.push({
                    entity,
                    errors: ['Invalid entity structure']
                });
            }
        });

        return { valid, invalid };
    }
}

/**
 * 实体序列化工具类
 */
export class EntitySerializer {
    /**
     * 实体转对象
     */
    static toObject<T extends any>(entity: T): any {
        if (!entity || typeof entity !== 'object') {
            return null;
        }
        return JSON.parse(JSON.stringify(entity));
    }

    /**
     * 对象转实体
     */
    static fromObject<T extends any>(
        EntityClass: new () => T, 
        obj: any
    ): T {
        return (EntityClass as any).fromObject(obj);
    }

    /**
     * 批量实体转对象
     */
    static batchToObject<T extends any>(entities: T[]): any[] {
        return entities.map(entity => this.toObject(entity));
    }

    /**
     * 批量对象转实体
     */
    static batchFromObject<T extends any>(
        EntityClass: new () => T,
        objects: any[]
    ): T[] {
        return objects.map(obj => (EntityClass as any).fromObject(obj));
    }
}

/**
 * 实体缓存管理器
 */
export class EntityCache {
    private static entityCache = new Map<string, any>();
    
    /**
     * 缓存实体
     */
    static cacheEntity(key: string, entity: any, ttl: number = 3600000): void {
        this.entityCache.set(key, {
            entity,
            timestamp: Date.now(),
            ttl
        });
    }
    
    /**
     * 获取缓存实体
     */
    static get<T>(key: string): T | null {
        const cached = this.entityCache.get(key);
        if (!cached) return null;
        
        if (Date.now() - cached.timestamp > cached.ttl) {
            this.entityCache.delete(key);
            return null;
        }
        
        return cached.entity;
    }
    
    /**
     * 清除缓存
     */
    static clear(pattern?: string): void {
        if (pattern) {
            const keys = Array.from(this.entityCache.keys()).filter((key: string) =>
                key.includes(pattern)
            );
            keys.forEach(key => this.entityCache.delete(key));
        } else {
            this.entityCache.clear();
        }
    }
}